package com.panda.rentHouse.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.panda.common.controller.BaseController;
import com.panda.rentHouse.pojo.User;
import com.panda.rentHouse.service.UserService;

@Controller
@RequestMapping(value="/rentHouse/user")
public class UserController extends BaseController{

	@Autowired
	private UserService userService;
	
	/**
	 * 查找所有
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/findAll")
	public Map<String,Object> findAll(HttpServletRequest req, HttpServletResponse res){
		logger.info("execute the UserController 's findAll method...");
		Map<String,Object> map = new HashMap<String, Object>();
		List<User> data = null;
		try {
			data = userService.findAll();
			map = super.getSuccessResultMap("ok", data, data.size());
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
		return map;
	}
	
	/**
	 * 登录
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/loginCheck")
	public Map<String,Object> loginCheck(HttpServletRequest req, HttpServletResponse res){
		logger.info("execute the UserController 's loginCheck method...");
		Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> data = null;
		try {
			String name = super.getString(req, "name", "");
			String password = super.getString(req, "password", "");
			System.out.println(name + ":" +  password);
			data = userService.loginCheck(name, password);
			map = super.getSuccessResultMap("ok", data);
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
		return map;
	}
	
}
